
class PeanutGun extends FFWeapon;

#exec MESH IMPORT MESH=PNutGunAnimated ANIVFILE=MODELS\PnutGunAnimated_a.3d DATAFILE=MODELS\PnutGunAnimated_d.3d X=0 Y=0 Z=0 UnMirror=1
#exec MESH ORIGIN MESH=PNutGunAnimated X=40 Y=-80 Z=0 YAW=-4 PITCH=2 ROLL=0

#exec MESH SEQUENCE MESH=PNutGunAnimated SEQ=All    STARTFRAME=0  NUMFRAMES=73
#exec MESH SEQUENCE MESH=PNutGunAnimated SEQ=Still  STARTFRAME=0  NUMFRAMES=1
#exec MESH SEQUENCE MESH=PNutGunAnimated SEQ=3Shot  STARTFRAME=2  NUMFRAMES=10
#exec MESH SEQUENCE MESH=PNutGunAnimated SEQ=Shoot  STARTFRAME=13 NUMFRAMES=5
#exec MESH SEQUENCE MESH=PNutGunAnimated SEQ=Reload STARTFRAME=19 NUMFRAMES=43
#exec MESH SEQUENCE MESH=PNutGunAnimated SEQ=Down   STARTFRAME=63 NUMFRAMES=7
#exec MESH SEQUENCE MESH=PNutGunAnimated SEQ=Select STARTFRAME=71 NUMFRAMES=7

#exec TEXTURE IMPORT NAME=PgunUVmap FILE=Textures/PgunUVmap.pcx GROUP="Skins" LODSET=2
#exec TEXTURE IMPORT NAME=HandGlove FILE=Textures/SimpleGloveHandTexture.pcx GROUP="Skins" LODSET=2

#exec MESHMAP NEW MESHMAP=PNutGunAnimated MESH=PNutGunAnimated
#exec MESHMAP SCALE MESHMAP=PNutGunAnimated X=0.0075 Y=0.0075 Z=0.015
#exec MESHMAP SETTEXTURE MESHMAP=PNutGunAnimated NUM=1 TEXTURE=HandGlove
#exec MESHMAP SETTEXTURE MESHMAP=PNutGunAnimated NUM=2 TEXTURE=PgunUVmap
#exec MESHMAP SETTEXTURE MESHMAP=PNutGunAnimated NUM=3 TEXTURE=HandGlove

// Left Hand
#exec MESH IMPORT MESH=PNutGunAnimatedL ANIVFILE=MODELS\PnutGunAnimated_a.3d DATAFILE=MODELS\PnutGunAnimated_d.3d
#exec MESH ORIGIN MESH=PNutGunAnimatedL X=0 Y=0 Z=0 YAW=128 PITCH=0 ROLL=0
#exec MESH SEQUENCE MESH=PNutGunAnimatedL SEQ=All    STARTFRAME=0  NUMFRAMES=73
#exec MESH SEQUENCE MESH=PNutGunAnimatedL SEQ=Still  STARTFRAME=1  NUMFRAMES=1
#exec MESH SEQUENCE MESH=PNutGunAnimatedL SEQ=3Shot  STARTFRAME=2  NUMFRAMES=10
#exec MESH SEQUENCE MESH=PNutGunAnimatedL SEQ=Shoot  STARTFRAME=13 NUMFRAMES=5
#exec MESH SEQUENCE MESH=PNutGunAnimatedL SEQ=Reload STARTFRAME=19 NUMFRAMES=43
#exec MESH SEQUENCE MESH=PNutGunAnimatedL SEQ=Down   STARTFRAME=63 NUMFRAMES=7
#exec MESH SEQUENCE MESH=PNutGunAnimatedL SEQ=Select STARTFRAME=71 NUMFRAMES=7

#exec MESHMAP SCALE MESHMAP=PNutGunAnimatedL X=0.01 Y=0.01 Z=0.02
#exec MESHMAP SETTEXTURE MESHMAP=PNutGunAnimatedL NUM=1 TEXTURE=HandGlove
#exec MESHMAP SETTEXTURE MESHMAP=PNutGunAnimatedL NUM=2 TEXTURE=PgunUVmap
#exec MESHMAP SETTEXTURE MESHMAP=PNutGunAnimatedL NUM=3 TEXTURE=HandGlove

// Pickup
#exec MESH IMPORT MESH=PeanutGunPickup ANIVFILE=MODELS\PnutGunPickUp_a.3d DATAFILE=MODELS\PnutGunPickUp_d.3d X=0 Y=0 Z=0 LODSTYLE=10 UnMirror=1
#exec MESH ORIGIN MESH=PeanutGunPickup X=0 Y=0 Z=0 YAW=0 PITCH=0 ROLL=0
#exec MESH SEQUENCE MESH=PeanutGunPickup SEQ=All STARTFRAME=0  NUMFRAMES=1
#exec MESH SEQUENCE MESH=PeanutGunPickup SEQ=Still STARTFRAME=0  NUMFRAMES=1
#exec MESH SEQUENCE MESH=PeanutGunPickup SEQ=Select STARTFRAME=0  NUMFRAMES=1
#exec MESHMAP NEW MESHMAP=PeanutGunPickup MESH=PeanutGunPickup
#exec MESHMAP SCALE MESHMAP=PeanutGunPickup X=0.3125 Y=0.3125 Z=0.625
#exec MESHMAP SETTEXTURE MESHMAP=PeanutGunPickup NUM=1 TEXTURE=PgunUVmap
#exec MESHMAP SETTEXTURE MESHMAP=PeanutGunPickup NUM=2 TEXTURE=PgunUVmap
#exec MESHMAP SETTEXTURE MESHMAP=PeanutGunPickup NUM=3 TEXTURE=PgunUVmap
#exec MESHMAP SETTEXTURE MESHMAP=PeanutGunPickup NUM=4 TEXTURE=PgunUVmap
#exec MESHMAP SETTEXTURE MESHMAP=PeanutGunPickup NUM=5 TEXTURE=PgunUVmap

#exec MESH IMPORT MESH=PeanutGunHand ANIVFILE=MODELS\PnutGunPickUp_a.3d DATAFILE=MODELS\PnutGunPickUp_d.3d X=0 Y=0 Z=0 LODSTYLE=10 UnMirror=1
#exec MESH ORIGIN MESH=PeanutGunHand X=0 Y=0 Z=10 YAW=0 PITCH=0 ROLL=0
#exec MESH SEQUENCE MESH=PeanutGunHand SEQ=All     STARTFRAME=0  NUMFRAMES=1
#exec MESHMAP NEW MESHMAP=PeanutGunHand MESH=PeanutGunHand
#exec MESHMAP SCALE MESHMAP=PeanutGunHand X=0.2 Y=0.2 Z=0.4
#exec MESHMAP SETTEXTURE MESHMAP=PeanutGunHand NUM=1 TEXTURE=PgunUVmap
#exec MESHMAP SETTEXTURE MESHMAP=PeanutGunHand NUM=2 TEXTURE=PgunUVmap
#exec MESHMAP SETTEXTURE MESHMAP=PeanutGunHand NUM=3 TEXTURE=PgunUVmap
#exec MESHMAP SETTEXTURE MESHMAP=PeanutGunHand NUM=4 TEXTURE=PgunUVmap
#exec MESHMAP SETTEXTURE MESHMAP=PeanutGunHand NUM=5 TEXTURE=PgunUVmap

#exec TEXTURE IMPORT NAME=IconMini FILE=TEXTURES\HUD\WpnMini.PCX GROUP="Icons" MIPS=OFF
#exec TEXTURE IMPORT NAME=UseMini FILE=TEXTURES\HUD\UseMini.PCX GROUP="Icons" MIPS=OFF

#exec AUDIO IMPORT FILE="Sounds\PNutFire.Wav"      NAME=PNutFire      GROUP="PNutGun"
#exec AUDIO IMPORT FILE="Sounds\PNutFire3Shot.Wav" NAME=PNutFire3Shot GROUP="PNutGun"
#exec AUDIO IMPORT FILE="Sounds\PNutPickup.Wav"    NAME=PNutPickup    GROUP="PNutGun"
#exec AUDIO IMPORT FILE="Sounds\PNutReload.Wav"    NAME=PNutReload    GROUP="PNutGun"

//Icon
#exec TEXTURE IMPORT NAME=PNutGunIcon FILE=Textures/PNutGunIcon.BMP GROUP="Icons" MIPS=OFF
#exec TEXTURE IMPORT NAME=FFAmmoClip FILE=Textures/FFAmmoClip.BMP GROUP="Icons"

var float ShotAccuracy, LastShellSpawn;
var int Count;
var bool bOutOfAmmo, bFiredShot;
var() texture MuzzleFlashVariations[10];
var float TimesShot;
var color HUDcolor;
var() int HitDamage;
var() int burstShots;   //burstShots=3
var int shotCounter;

function setHand(float Hand)
{
	if ( Hand == 2 )
	{
		FireOffset.Y = 0;
		bHideWeapon = true;
		return;
	}
	else
		bHideWeapon = false;
	PlayerViewOffset = Default.PlayerViewOffset * 100;
	FireOffset.Y = Hand * Default.FireOffset.Y;
	PlayerViewOffset.Y *= Hand;
	if ( Hand == 1 )
		Mesh = mesh(DynamicLoadObject("Botpack.PeanutGunAnimL", class'Mesh'));
	else
	{
		Mesh = mesh'PNutGunAnimated';
		if ( Hand == 0 )
		{
			PlayerViewOffset.X = Default.PlayerViewOffset.X * 95;
			PlayerViewOffset.Z = Default.PlayerViewOffset.Z * 105;
		}
	}
}

function float RateSelf( out int bUseAltMode )
{
	local float dist;

	if ( AmmoType.AmmoAmount <=0 )
		return -2;

	if ( Pawn(Owner).Enemy == None )
	{
		bUseAltMode = 0;
		return AIRating;
	}

	dist = VSize(Pawn(Owner).Enemy.Location - Owner.Location); 
	bUseAltMode = 0;
	if ( dist > 1200 )
	{
		if ( dist > 1700 )
			bUseAltMode = 0;
		return (AIRating * FMin(Pawn(Owner).DamageScaling, 1.5) + FMin(0.0001 * dist, 0.3)); 
	}
	AIRating *= FMin(Pawn(Owner).DamageScaling, 1.5);
	return AIRating;
}

simulated event RenderOverlays( canvas Canvas )
{
	local vector X,Y,Z;
	local float dir;

	if ( bSteadyFlash3rd )
	{
		bMuzzleFlash = 1;
		bSetFlashTime = false;
		if ( !Level.bDropDetail )
			MFTexture = MuzzleFlashVariations[Rand(10)];
		else
			MFTexture = MuzzleFlashVariations[Rand(5)];
	}
	else
		bMuzzleFlash = 0;

	Super.RenderOverlays(Canvas);
}

function TraceFire( float Accuracy )
{
	local vector HitLocation, HitNormal, StartTrace, EndTrace, X,Y,Z, AimDir;
	local actor Other;

	Owner.MakeNoise(Pawn(Owner).SoundDampening);
	GetAxes(Pawn(owner).ViewRotation,X,Y,Z);
	StartTrace = Owner.Location + CalcDrawOffset() + FireOffset.Y * Y + FireOffset.Z * Z; 
	AdjustedAim = pawn(owner).AdjustAim(1000000, StartTrace, 2.75*AimError, False, False);	
	EndTrace = StartTrace + Accuracy * (FRand() - 0.5 )* Y * 1000 + Accuracy * (FRand() - 0.5 ) * Z * 1000;
	AimDir = vector(AdjustedAim);
	EndTrace += (10000 * AimDir); 
	Other = Pawn(Owner).TraceShot(HitLocation,HitNormal,EndTrace,StartTrace);

	Count++;
	if ( Count == 1 )
	{
		Count = 0;
		if ( VSize(HitLocation - StartTrace) > 100 )
			Spawn(class'PNutTracer',,, StartTrace + 96 * AimDir,rotator(EndTrace - StartTrace));
	}
	ProcessTraceHit(Other, HitLocation, HitNormal, vector(AdjustedAim),Y,Z);
}

function ProcessTraceHit(Actor Other, Vector HitLocation, Vector HitNormal, Vector X, Vector Y, Vector Z)
{
	if ( (Other!=self) && (Other!=Owner) && (Other != None) )
	{
		if ( Other.IsA('Bot') && (FRand() < 0.2) )
			Pawn(Other).WarnTarget(Pawn(Owner), 500, X);
		Other.TakeDamage(HitDamage, Pawn(Owner), HitLocation, 6000.0*X, MyDamageType);
	}
	ProcessTraceHitEffect(Other, ConsumableAmmo(AmmoType), HitLocation, HitNormal);
}

function Fire( float Value )
{
	if ( AmmoType == None )
	{
		GiveAmmo(Pawn(Owner));
	}
	if ( AmmoType.UseAmmo(1) )
	{
      GotoState('NormalFire');
		Pawn(Owner).PlayRecoil(FiringSpeed);
		bCanClientFire = true;
		bPointing=True;
		ShotAccuracy = 0.4;
		ClientFire(value);
		TraceFire(0.95);
	}
}

function AltFire( float Value )
{
	if ( AmmoType == None )
	{
		GiveAmmo(Pawn(Owner));
	}
	if ( AmmoType.UseAmmo(1) )
	{
      GotoState('AltFiring');
		Pawn(Owner).PlayRecoil(FiringSpeed);
		bCanClientFire = true;
		bPointing=True;
		ShotAccuracy = 0.1;
		ClientAltFire(value);
		TraceFire(0.1);
	}
}

state ClientAltFiring
{
   simulated function AnimEnd()
   {
      shotCounter--;
      if (shotCounter > 0)
         PlayAltFiring();
      else if (Pawn(Owner) != None && Pawn(Owner).bAltFire != 0)
         TweenToStill();
      else
         Super.AnimEnd();
   }
   
   simulated function BeginState()
   {
      shotCounter = Min(AmmoType.AmmoAmount - 1, burstShots);
      Super.BeginState();
   }
}

simulated function PlayFiring()
{
	TimesShot = TimesShot - 1;
	PlayOwnedSound(FireSound, SLOT_None, Pawn(Owner).SoundDampening*3.0);
	if ( PlayerPawn(Owner) != None )
		PlayerPawn(Owner).ShakeView(ShakeTime, ShakeMag, ShakeVert);
	PlayAnim('Shoot',2.2, 0.1);
	bSteadyFlash3rd = true;
	If (TimesShot <= 0)
	{
		TimesShot = 30;
		PlayReloading();
	}
}

simulated function PlayAltFiring()
{
   TimesShot = TimesShot - 1;
   PlayOwnedSound(FireSound, SLOT_None, Pawn(Owner).SoundDampening*3.0);
	if ( PlayerPawn(Owner) != None )
		PlayerPawn(Owner).ShakeView(ShakeTime, ShakeMag, ShakeVert);
	PlayAnim('Shoot',1 + 1.6 * FireAdjust, 0.1);
	bSteadyFlash3rd = False;
	If (TimesShot <= 0)
	{
		TimesShot = 30;
		PlayReloading();
	}
}

state NormalFire
{
	ignores Fire, AltFire;
	
	function BeginState()
	{
		AmbientGlow = 250;
		AmbientSound = FireSound;
		bSteadyFlash3rd = true;
		Super.BeginState();
	}	

	function EndState()
	{
		bSteadyFlash3rd = false;
		AmbientGlow = 0;
		LightType = LT_None;
		AmbientSound = None;
		Super.EndState();
	}

Begin:
	FinishAnim();
	if ( (AmmoType != None) && (AmmoType.AmmoAmount<=0))
	{
		Gotostate('Idle');
	}
	Finish();
}

state AltFiring
{
   function AnimEnd()
   {
      shotCounter--;
      if (shotCounter > 0)
      {
         AmmoType.UseAmmo(1);
         TraceFire(0.1);
         PlayAltFiring();
      }
      else if (Pawn(Owner) != None && Pawn(Owner).bAltFire != 0)
         TweenToStill();
      else
         Super.AnimEnd();
   }
   
   function BeginState()
   {
      shotCounter = Min(AmmoType.AmmoAmount, burstShots);
      Super.BeginState();
   }
}

state Idle
{

Begin:
	if (Pawn(Owner).bFire!=0 && AmmoType.AmmoAmount>0) Fire(0.0);
	if (Pawn(Owner).bAltFire!=0 && AmmoType.AmmoAmount>0) AltFire(0.0);	
	LoopAnim('Still',0.2,0.9);
	bPointing=False;
	if ( (AmmoType != None) && (AmmoType.AmmoAmount<=0) ) 
		Pawn(Owner).SwitchToBestWeapon();
	Disable('AnimEnd');
}

simulated function PlayReloading()
{
	PlayOwnedSound(Sound'PNutReload', SLOT_None,2.0*Pawn(Owner).SoundDampening);
	PlayAnim('Reload',0.5,0.05);
	Gotostate('Reloading');
}

simulated function PostRender(canvas C)
{
	local PlayerPawn P;
   local float Scale;
   local font CanvasFont;

   Super.PostRender(C);
   P = PlayerPawn(Owner);
   if ( (P != None) )
   {
		Scale = C.ClipX/640;
      C.SetPos(C.ClipX - 32 * Scale, C.ClipY - 192 * Scale);
      C.Style = ERenderStyle.STY_Translucent;
		C.DrawIcon(Texture'FFAmmoClip', Scale/2);
      C.Font = C.LargeFont;
		C.SetPos(C.ClipX - 22 * Scale, C.ClipY - 180 * Scale);
		C.DrawText(int(TimesShot), false);
	}
}

defaultproperties
{
   MuzzleFlashVariations(0)=Texture'Botpack.Skins.Muz1'
   MuzzleFlashVariations(1)=Texture'Botpack.Skins.Muz2'
   MuzzleFlashVariations(2)=Texture'Botpack.Skins.Muz3'
   MuzzleFlashVariations(3)=Texture'Botpack.Skins.Muz4'
   MuzzleFlashVariations(4)=Texture'Botpack.Skins.Muz5'
   MuzzleFlashVariations(5)=Texture'Botpack.Skins.Muz6'
   MuzzleFlashVariations(6)=Texture'Botpack.Skins.Muz7'
   MuzzleFlashVariations(7)=Texture'Botpack.Skins.Muz8'
   MuzzleFlashVariations(8)=Texture'Botpack.Skins.Muz9'
   MuzzleFlashVariations(9)=Texture'Botpack.Skins.Muz9'
   WeaponDescription="Classification: Peanut Gun"
   AmmoName=Class'FoodFightWeapons.PeanutCan'
   PickupAmmoCount=40
   bInstantHit=True
   bAltInstantHit=True
   bRapidFire=True
   FireOffset=(X=8.000000,Y=-5.000000,Z=-4.000000)
   MyDamageType=shot
   hitdamage=25
   shakemag=135.000000
   shakevert=8.000000
   AIRating=0.730000
   RefireRate=0.990000
   AltRefireRate=0.350000
   FireSound=Sound'PNutFire'
   AltFireSound=Sound'PNutFire3Shot'
   SelectSound=Sound'UnrealI.Minigun.MiniSelect'
   Misc1Sound=None
   DeathMessage="%k's %w turned %o into a leaky sausage with peanuts."
   NameColor=(B=0)
   bDrawMuzzleFlash=True
   MuzzleScale=2.000000
   FlashY=0.100000
   FlashO=0.016000
   FlashC=0.006000
   FlashLength=0.200000
   FlashS=128
   MFTexture=Texture'Botpack.Skins.Muz9'
   AutoSwitchPriority=7
   InventoryGroup=7
   PickupMessage="You got the Peanut gun."
   ItemName="Peanut Gun"
   PlayerViewOffset=(X=2.100000,Y=-0.350000,Z=-1.700000)
   PlayerViewMesh=LodMesh'PNutGunAnimated'
   BobDamping=0.975000
   PickupViewMesh=LodMesh'FoodFightWeapons.PeanutGunPickup'
   ThirdPersonMesh=LodMesh'FoodFightWeapons.PeanutGunHand'
   StatusIcon=Texture'PNutGunIcon'
   bMuzzleFlashParticles=True
   MuzzleFlashStyle=STY_Translucent
   MuzzleFlashMesh=LodMesh'Botpack.MuzzFlash3'
   MuzzleFlashScale=0.150000
   MuzzleFlashTexture=Texture'Botpack.Skins.Muzzy'
   PickupSound=Sound'PNutPickup'
   Icon=Texture'PNutGunIcon'
   Mesh=LodMesh'FoodFightWeapons.PeanutGunPickup'
   bNoSmooth=False
   SoundRadius=96
   SoundVolume=255
   CollisionRadius=34.000000
   CollisionHeight=8.000000
   LightEffect=LE_NonIncidence
   LightBrightness=255
   LightHue=28
   LightSaturation=32
   LightRadius=6
   TimesShot=30
   burstShots=3
}
